﻿using SGArchiveSystem7xDB;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;

namespace SGArchiveSystem7x.Controllers
{
    public class LoginController : Controller
    {
        /// <summary>
        /// 登录控制器
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            if (Models.Language._nowLanguage == _VersionLanguage._Chs)
            {
                ViewBag.AppTitle = SGArchiveSystem7xCommons.SGWebInfo._sAppTitle;
                ViewBag.Text1 = "请输入账号";
                ViewBag.Text2 = "请输入密码";
                ViewBag.Text3 = "登录";
                ViewBag.Text4 = "建议分辨率：1280*1024 或以上";
                ViewBag.Text5 = SGArchiveSystem7xCommons.SGWebInfo._sAppFooterContent;
                ViewBag.Text6 = "用户名或密码不能为空!";
            }
            else
            {
                ViewBag.AppTitle = SGArchiveSystem7xCommons.SGWebInfo._sAppTitle_Eng;
                ViewBag.Text1 = "enter user name";
                ViewBag.Text2 = "enter user password";
                ViewBag.Text3 = "Login";
                ViewBag.Text4 = "Recommended resolution: 1280*1024 or more";
                ViewBag.Text5 = SGArchiveSystem7xCommons.SGWebInfo._sAppFooterContent_Eng;
                ViewBag.Text6 = "User name or password cannot be empty";
            }
            
            return View();
        }

       
        [HttpPost]
        public string UserLogin(string name, string pwd)
        {
            //string uname= System.Environment.UserName;//当前登录域的用户
            //string strHostName = Dns.GetHostName();
            //IPHostEntry ipEntry = Dns.GetHostByName(strHostName);
            // string ip = ipEntry.AddressList[0].ToString();
            //string HostName = string.Empty;
            //string ip = string.Empty;
            //string ipv4 = String.Empty;
            string ipv4 = Request.UserHostAddress;
            //if (!string.IsNullOrEmpty(System.Web.HttpContext.Current.Request.ServerVariables["HTTP_VIA"]))
            //    ip = Convert.ToString(System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]);
            //if (string.IsNullOrEmpty(ip))
            //    ip = Request.UserHostAddress;

            //// 利用 Dns.GetHostEntry 方法，由获取的 IPv6 位址反查 DNS 纪录，<br> // 再逐一判断何者为 IPv4 协议，即可转为 IPv4 位址。
            //foreach (IPAddress ipAddr in Dns.GetHostEntry(ip).AddressList)
            //{
            //    if (ipAddr.AddressFamily.ToString() == "InterNetwork")
            //    {
            //        ipv4 = ipAddr.ToString();
            //    }
            //}
            //HostName = "主机名: " + Dns.GetHostEntry(ip).HostName + " IP: " + ipv4;
            string PWD = SGArchiveSystem7xCommons.PWD_MD5.MD5Encrypt(pwd);
            var isuser= SGArchiveSystem7xDB.Linq.T_UserDal.GetLoginUser(name, null);
            ///var user = SGArchiveSystem7xDB.Linq.T_UserDal.GetLoginUser(name, pwd);
            if (isuser != null)
            {
                //if (isuser != null)
                {
                    if(isuser.U_STATUS==0)
                        return "账号不可用，请联系管理员！";
                    if (isuser.U_ISIP == 1)//绑了ip
                    {
                        if (isuser.U_AD == 1)//ad登录( ad登录这块这块永远不去执行 )
                        {
                            if (isuser.U_IP == ipv4)
                            {
                                if (SGArchiveSystem7xDB.Linq.T_ADDomainDal.IsConnected("11.11.10.28", name, pwd))
                                {
                                    SucessLogin(isuser, ipv4);
                                    return "true";
                                }
                                else
                                    return "账号或密码错误";
                            }
                            else
                            {
                                return "ip不符";
                            }
                        }
                        else//普通登录
                        {
                            if (isuser.U_IP == ipv4)
                            {
                                if (isuser.U_PWD != PWD)
                                    return "账号或密码错误";
                                else
                                {
                                    SucessLogin(isuser, ipv4);
                                    return "true";
                                }
                            }
                            else
                            {
                                return "ip不符";//
                            }
                        }
                    }
                    else//没绑ip
                    {
                        if (isuser.U_AD == 1)//ad登录( ad登录这块这块永远不去执行 )
                        {

                            if (SGArchiveSystem7xDB.Linq.T_ADDomainDal.IsConnected("11.11.10.28", name, pwd))
                            {
                                SucessLogin(isuser, ipv4);
                                return "true";
                            }
                            else
                                return "账号或密码错误";
                        }
                        else//普通登录
                        {
                            if (isuser.U_PWD != PWD)
                                return "账号或密码错误";
                            else
                            {
                                SucessLogin(isuser, ipv4);
                                return "true";
                            }
                        }
                    }
                }
                //else
                //{
                //    return "账号或密码错误";//
                //}
            }
            else
            {
                return "没有用户";//
            }

        }
        private int SucessLogin(SGArchiveSystem7xDB.V_User user,string ip)
        {
            try
            {

                SGArchiveSystem7xDB.T_User en = new SGArchiveSystem7xDB.T_User();
                en.U_ID = user.U_ID;
                en.U_LASTLOGINIP = ip;
                en.U_LASTLOGINDATETIME =DateTime.Now;
                SGArchiveSystem7xDB.Linq.T_UserDal.Modify_User(en);
                App_Code.CSession.Add_User(Session, user);
                SGArchiveSystem7xDB.T_LogLogin en1 = new SGArchiveSystem7xDB.T_LogLogin();
                en1.LOG_UID= user.U_ID;
                en1.LOG_DATE = DateTime.Now;
                en1.LOG_IP = ip;
                if (user.U_AD == 1)
                    en1.LOG_TYPE = (int)_LoginType.AD;
                else
                    en1.LOG_TYPE = (int)_LoginType.Normal;
                SGArchiveSystem7xDB.Linq.T_LogLoginDal.Add_LogLogin(en1);


                /////添加全局存储
                List<string[]> list = new List<string[]>();
                if (System.Web.HttpContext.Current.Application["SgOnline"] != null)
                {
                    list = System.Web.HttpContext.Current.Application["SgOnline"] as List<string[]>;
                    bool bAdd = false;
                    foreach (var ss in list)
                    {
                        if (ss[0] == Session.SessionID)
                            break;
                        else
                        {

                        }
                    }
                }
                else
                {
                    ////将本计算机的SESSION ID与登录的ID一起存进去
                    list.Add(new string[] { Session.SessionID, en.U_ID.ToString() });
                }

                return 1;
            }
            catch
            {
                return -1;
            }

        }
    }
}